﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace XDataUtils.SchemaFiledData
{
	public class XDataManager
	{
		public XDataManager(SchemaManager schemaMgr)
		{
			this.m_schemaManager = schemaMgr;
		}

		public Schema FinishSchema()
		{
			return this.m_schemaManager.CreateSchema();
		}

		public void SetElementEntity<T>(Element ele, string strFieldName, T tValue, DisplayUnitType showUnitType, ref Entity entity)
		{
			Field field = this.m_schemaManager.GetSchema().GetField(strFieldName);
			if (field.IsValidObject)
			{
				entity.Set<T>(field, tValue, showUnitType);
			}
		}

		public void SetElementXData(Element ele, Entity entity)
		{
			ele.SetEntity(entity);
		}

		public Entity CreateEntity()
		{
			return new Entity(this.m_schemaManager.GetSchema());
		}

		public static T GetElementXData<T>(Guid guid, Element ele, string FieldName, DisplayUnitType showUnitType)
		{
			Schema schema = SchemaManager.GetSchema(guid);
			return ele.GetEntity(schema).Get<T>(schema.GetField(FieldName), 0);
		}

		private SchemaManager m_schemaManager;
	}
}
